package com.wang.interview.threads;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author: wang
 * @date: 2021/7/24 21:44
 * @description: 阻塞队列，ArrayBlockingQueue, SetBlockingQueue, SynchronousQueue
 *     异常       无异常    阻塞     过时不候
 *添加  add       offer     put     offer(time)
 *获取  remove    poll     take     poll(time)
 *检查  element   peek     null      null
 */
public class BlockIngQueenTest {

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<Integer> integers = new ArrayBlockingQueue<>(3);
        /*integers.add(1);
        integers.add(2);
        integers.add(3);*/
        /*System.out.println(1);
        integers.add(4);
        System.out.println(2);*/  // add
        /*integers.remove();
        integers.remove();
        integers.remove();*/  // java.util.NoSuchElementException
        // System.out.println(integers.add(1)); // true
        // System.out.println(integers.remove(1)); // false,不会报异常
        // System.out.println(integers.element());  // 获取第一个元素 没有报NoSuchElementException

        /*System.out.println(integers.offer(1));
        System.out.println(integers.offer(1));
        System.out.println(integers.offer(1)); // true
        System.out.println(integers.offer(1)); // false

        System.out.println(integers.poll());
        System.out.println(integers.poll());
        System.out.println(integers.poll()); // 1
        System.out.println(integers.poll()); // null*/

        /*integers.offer(1);
        integers.offer(1);
        System.out.println(integers.peek());  // 返回头元素，没有就返回null*/

        /*integers.put(1);
        integers.put(1);
        integers.put(1);*/
        //integers.put(1);   // 阻塞

        /*System.out.println(integers.take());
        System.out.println(integers.take());
        System.out.println(integers.take());*/
        // System.out.println(integers.take());  // 阻塞

        System.out.println(integers.offer(1));
        System.out.println(integers.offer(1));
        System.out.println(integers.offer(1));
        System.out.println(integers.offer(1, 5,TimeUnit.SECONDS));  // 阻塞到时间就会返回false


    }
}
